<?php 
/**
* @package DomBase
* @author Oliver Leisalu
* {@code 
$pager = new DomPager( $total, $perPage, $link );
// returns page numbers in array
$pages = $pager->getPages();

@}
*/
class DomPager {
	
	/**
	* $_GET key where page number is taken from.
	*/
	protected $var = 'lk';
	public $urlSeperator = '&amp;';
	public $urlEqual = '=';
	public $urlEnd = '';
	
	protected $currentPage;
	protected $total;
	protected $perPage;
	protected $link;
	protected $totalPages;
	
	/**
	* if page numbers are large, then how many pages are shown from left and right side. (for value 2 pages are: 1,2,...,x,...,n-1,n)
	*/
	public $showSides = 5;
	/**
	* how many numbers are shown in middle, when page numbers are large. (for value 4 pages are: x,...,20,21,22,23,...,y)
	*/
	public $showMiddle = 4;
	/**
	* seperator from groups when page nubmers are large.
	*/
	public $seperator = '...';
	
	/**
	* @param Int $total
	* @param Int $perPage
	* @param String $link - default link (like http://sample.com/galleryId,10;)
	* Notice that url rewriting is used. 
	*/
	public function __construct( $total, $perPage, $link ) {
		$this->total = $total;
		$this->perPage = $perPage;
		$this->link = $link;
		$this->totalPages = ceil( $total / $perPage );
		$this->currentPage = (int)$_GET[$this->var];
	}
	
	/**
	* returns limit for sql queries.
	* @return String "Int,Int"
	*/
	public function getLimit() {
		return ((int)$this->currentPage*$this->perPage).','.(int)$this->perPage;
	}
	
	/**
	* set total number of entries
	* @param Int $total
	*/
	public function setTotal( $total) {
		$this->total = $total;
		$this->totalPages = ceil( $total / $this->perPage );
	}
	
	/**
	* Set current page number manualy. 
	*/
	public function setCurrentPage( $page ) {
		$this->currentPage = (int)$page;
	}
	
	/**
	* @return Int records start
	*/
	public function getStart() {
		return (int)($this->currentPage*$this->perPage);
	}
	
	/**
	* @return Int total records
	*/
	public function getTotal() {
		return $this->total;
	}
	
	/**
	* @return Int total number of pages
	*/ 
	public function getTotalPages() {
		return $this->totalPages;
	}
	
	/**
	* @return Int records per page
	*/
	public function getPerPage() {
		return $this->perPage;
	}
	
	/**
	* @return Int current page
	*/
	public function getCurrentPage() {
		return (int)$this->currentPage;
	}
	
	/**
	* Set url variable name
	*/
	public function setVar( $var ) {
		$this->var = $var;
		$this->currentPage = (int)$_GET[$this->var];
	}
	
	/**
	* If data will be on single page or not.
	* @return Bool
	*/
	public function hasPages() {
		return $this->total > $this->perPage;
	}
	/**
	* @return array(int=>array(name=>Int(page number), link=>String, selected=>boolean))
	*/
	public function getPages() {
		if( $this->total > $this->perPage) {
			$pages = array();
			for( $i=0; $i<$this->totalPages; $i++) {
				if( $i < $this->showSides or $i > $this->totalPages-$this->showSides or $i+$this->showMiddle > $this->currentPage && $i-$this->showMiddle < $this->currentPage )
					$pages[] = array('name'=>$i+1, 'link'=>$this->link.$this->urlSeperator.$this->var.$this->urlEqual.$i.$this->urlEnd, 'selected'=>(boolean)($i == $this->currentPage) );
				else if( $i == $this->showSides && $this->currentPage >= $this->showSides+$this->showMiddle or $i == $this->totalPages-$this->showSides && $this->currentPage <= $this->totalPages-$this->showSides-$this->showMiddle )
					$pages[] = array('name'=>$this->seperator);
			}
			return $pages;
		} else {
			return array();
		}
	}
	
	/**
	* @return String link for next page
	*/
	public function getNextLink() {
		if( $this->currentPage+1 < $this->totalPages ) 
			return $this->link.$this->urlSeperator.$this->var.$this->urlEqual.($this->currentPage+1);
		else
			return false;
	}
	
	/**
	* @return String link for previous page 
	*/
	public function getPrevLink() {
		if( $this->currentPage-1 >= 0 ) 
			return $this->link.$this->urlSeperator.$this->var.$this->urlEqual.($this->currentPage-1);
		else
			return false;
	}

}

?>